//
//  DatabaseMigrationTests.m
//  JKOCRealmTests
//
//  Created by 王冲 on 2019/2/27.
//  Copyright © 2019年 JK科技有限公司. All rights reserved.
//

#import <XCTest/XCTest.h>
#import "RLMRealmDataBaseTools.h"
#import "MigrationModel.h"
@interface DatabaseMigrationTests : XCTestCase

@end

@implementation DatabaseMigrationTests

- (void)setUp {
    
   // [RLMRealmDataBaseTools databaseMigration:4];
    
    // 1. 获取默认配置
    RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
    
    int newVersion = 6;
    // 2. 叠加版本号(要比上一次的版本号高) 0
    config.schemaVersion = newVersion;
    
    // 3. 具体怎样迁移
    [config setMigrationBlock:^(RLMMigration *migration, uint64_t oldSchemaVersion) {
        
        //什么都不要做！Realm 会自行检测新增和需要移除的属性，然后自动更新硬盘上的数据库架构
        if (oldSchemaVersion < newVersion) {
            
            NSLog(@"需要做迁移动作");
            
            //执行更名动作
            [migration renamePropertyForClass:@"MigrationModel" oldName:@"age" newName:@"age1"];
            
            // 无需做任何事情, 就可以完成, 数据结构, 以及数据的迁移
            //                [migration enumerateObjects:@"Migration" block:^(RLMObject * _Nullable oldObject, RLMObject * _Nullable newObject) {
            //
            //                    newObject[@"fullName"] = [NSString stringWithFormat:@"%@%@", oldObject[@"preName"], oldObject[@"lastName"]];
            //                }];
        }
        
    }];
    
    // 4. 让配置生效
    [RLMRealmConfiguration setDefaultConfiguration:config];
    
    // 如果需要立即迁移
    [RLMRealm defaultRealm];
    
}

- (void)testExample1{
    
    MigrationModel *model = [[MigrationModel alloc]init];
    model.number = 6;
    model.name = @"小王";
    model.age1 = 19;
    
    RLMRealm *realm = [RLMRealm defaultRealm];
    [realm transactionWithBlock:^{
       
        [realm addObject:model];
    }];
}
    


@end
